#ifndef __FENCE_GATE_BLOCK_H__
#define __FENCE_GATE_BLOCK_H__

#include "Block.hpp"
#include "IPaintableBlock.hpp"
#include "Engine/Point3.hpp"
#include "Game/CellFace.h"
#include "Terrain/Terrain.h"
#include "Managers/BlocksManager.h"
#include "Components/ComponentMiner.h"
#include "Components/ComponentCreature.h"
#include "Components/ComponentCreatureModel.h"
#include "Subsystems/SubsystemTerrain.h"

using namespace PocketSurvival::Engine;

namespace PocketSurvival
{
    // 栅栏门方块基类 未完成...  未添加电路支持
    class FenceGateBlock : public Block, public IPaintableBlock
    {
    public:
		FenceGateBlock();
        virtual void getCreativeValues(std::vector<int32_t> &blockVec) override;
        virtual void getDropValues(int32_t oldValue, int32_t newValue, int32_t toolLevel, std::vector<BlockDropValue> &dropValues) override;
        virtual BlockPlacementData getPlacementValue(ComponentMiner *componentMiner, int32_t value, const TerrainRaycastResult &raycastResult) override;

        virtual int32_t getPaintColor(int32_t value) override;
        virtual int32_t paint(int32_t value, int32_t color) override;
        
		static inline int32_t GetRotation(int32_t data);
		static inline bool GetOpen(int32_t data);
		static inline bool GetRightHanded(int32_t data);
		static inline int32_t SetRotation(int32_t data, int32_t rotation);
		static inline int32_t SetOpen(int32_t data, bool open);
		static inline int32_t SetRightHanded(int32_t data, bool rightHanded);
		static inline int32_t GetHingeFace(int32_t data);

        static inline int32_t GetColor(int32_t data);
        static inline int32_t SetColor(int32_t data, int32_t color);
		static inline int32_t GetVariant(int32_t data);
		static inline int32_t SetVariant(int32_t data, int32_t variant);

    };

	inline int32_t FenceGateBlock::GetRotation(int32_t data)
	{
		return data & 3;
	}

	inline bool FenceGateBlock::GetOpen(int32_t data)
	{
		return (data & 4) != 0;
	}

	inline bool FenceGateBlock::GetRightHanded(int32_t data)
	{
		return (data & 8) == 0;
	}

	inline int32_t FenceGateBlock::SetRotation(int32_t data, int32_t rotation)
	{
		return (data & -4) | (rotation & 3);
	}

	inline int32_t FenceGateBlock::SetOpen(int32_t data, bool open)
	{
		if (!open)
		{
			return data & -5;
		}
		return data | 4;
	}

	inline int32_t FenceGateBlock::SetRightHanded(int32_t data, bool rightHanded)
	{
		if (rightHanded)
		{
			return data & -9;
		}
		return data | 8;
	}

	inline int32_t FenceGateBlock::GetHingeFace(int32_t data)
	{
		int32_t rotation = GetRotation(data);
		int32_t num = (rotation - 1 < 0) ? 3 : (rotation - 1);
		if (false == GetRightHanded(data))
		{
			num = CellFace::OppositeFace(num);
		}
		return num;
	}

    inline int32_t FenceGateBlock::GetColor(int32_t data)
    {
		if ((data & 0x20) != 0)
		{
			return (data >> 6) & 0xF;
		}
		return -1;
    }
    inline int32_t FenceGateBlock::SetColor(int32_t data, int32_t color)
    {
		if (color != -1)
		{
			return (data & -993) | 0x20 | ((color & 0xF) << 6);
		}
		return data & -993;
    }
	inline int32_t FenceGateBlock::GetVariant(int32_t data)
	{
		return data & 0x1F;
	}
	inline int32_t FenceGateBlock::SetVariant(int32_t data, int32_t variant)
	{
		return (data & -16) | (variant & 0xF);
	}

} // namespace PocketSurvival


#endif
